package com.dji.pa.cluster.db.dataSouce;

public class CommonUtils {
    /**
	 * 根据url判断数据类型
	 *
	 * @param templetDriverUrl
	 * @return
	 */
	public static String getIPAndPort(String templetDriverUrl) {
		if (templetDriverUrl == null) {
			return null;
		}
		if (templetDriverUrl.startsWith("jdbc:mysql:") || templetDriverUrl.startsWith("jdbc:cobar:")
				|| templetDriverUrl.startsWith("jdbc:log4jdbc:mysql:")) {
			return templetDriverUrl.substring(13, 27);
		} else if (templetDriverUrl.startsWith("jdbc:oracle:")
				|| templetDriverUrl.startsWith("jdbc:log4jdbc:oracle:")) {
			return templetDriverUrl.substring(18, 33);
		} else {
			throw new IllegalArgumentException("不支持的数据库类型");
		}
	}

	/*
	 * 只处理与通讯不可用引起的异常
	 */
	public static boolean isCommunicationsException(Throwable error) {
		String exceptionMsg = error.getCause().toString();
		return exceptionMsg.indexOf("CannotGetJdbcConnectionException") != -1
				|| exceptionMsg.indexOf("CommunicationsException") != -1
				|| exceptionMsg.indexOf("DataSourceClosedException") != -1
				//|| exceptionMsg.indexOf("GetConnectionTimeoutException") != -1
				|| exceptionMsg.indexOf("DataSourceDisableException") != -1
				|| exceptionMsg.indexOf("DataSourceNotAvailableException") != -1
				|| exceptionMsg.indexOf("GetConnectionTimeoutException") != -1;
	}
}
